package com.gzhryc.shared_device.oem.h5.mng.controllers.agent;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentLevel;
import com.gzhryc.shared_device.oem.code.agent.services.AgentLevelService;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentLevelSearch;
import com.gzhryc.shared_device.oem.code.config.BusinessConfig;

public class AgentLevelController {

    @Route(value = { "/agent/level/list.html" })
    public String list(HttpServletRequest request) {
        return "/agent/level/list.html";
    }

    @Route(value = "/agent/level/list.data")
    public ListResult<AgentLevel> listData(HttpServletRequest request) {
        ListResult<AgentLevel> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        AgentLevelSearch search = new AgentLevelSearch();
        result.getBody().setData(AgentLevelService.self().findBySearch(search));
        if (result.getBody().getData() != null) {
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/agent/level/add.html" })
    public String add(HttpServletRequest request) {
        Integer num = AgentLevelService.self().getByMaxNum() + 1;
        request.setAttribute("num",num);
        BusinessConfig config = ConfigUtils.getBusinessConfig();
    	request.setAttribute("serviceChargeRatio", config.getServiceChargeRatio());
        return "/agent/level/edit.html";
    }

    @Route(value = { "/agent/level/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        AgentLevel agentLevel = WebUtils.getParameter(request, AgentLevel.class);
        try {
        	if(agentLevel.getServiceChargeRatio() == null || agentLevel.getServiceChargeRatio() < 0 || agentLevel.getServiceChargeRatio() >= 100) {
                throw new LogicException("请输入正确的平台服务费分成百分比");
            }
            if(AgentLevelService.self().add(agentLevel,false)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/agent/level/update.html" })
    public String update(HttpServletRequest request) {
        Integer num = WebUtils.getInteger(request,"num");
        if(num != null){
            AgentLevel agentType = AgentLevelService.self().getByNum(num);
            WebUtils.setAttribute(request,agentType);
        }
        return "/agent/level/edit.html";
    }

    @Route(value = { "/agent/level/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败，请稍后再试");
        AgentLevel agentLevel = WebUtils.getParameter(request, AgentLevel.class);
        try {
        	if(agentLevel.getServiceChargeRatio() == null || agentLevel.getServiceChargeRatio() < 0 || agentLevel.getServiceChargeRatio() >= 100) {
                throw new LogicException("请输入正确的平台服务费分成百分比");
            }
            if(AgentLevelService.self().updateNotNull(agentLevel)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/agent/level/jump/add.html" })
    public String jumpAdd(HttpServletRequest request) {
    	BusinessConfig config = ConfigUtils.getBusinessConfig();
    	request.setAttribute("serviceChargeRatio", config.getServiceChargeRatio());
        return "/agent/level/edit.html";
    }

    @Route(value = { "/agent/level/jump/add.do" })
    public AjaxResult doJumpAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        AgentLevel agentLevel = WebUtils.getParameter(request, AgentLevel.class);
        try {
        	if(agentLevel.getServiceChargeRatio() == null || agentLevel.getServiceChargeRatio() < 0 || agentLevel.getServiceChargeRatio() >= 100) {
                throw new LogicException("请输入正确的平台服务费分成百分比");
            }
            if(AgentLevelService.self().add(agentLevel,true)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/agent/level/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        Integer num = WebUtils.getInteger(request,"num");
        try {
            if(AgentLevelService.self().deleteByNum(num)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
